package com.ss.android.article.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

import com.a.a.NightMode;
import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.NetworkType;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.browser.R;
import com.ss.android.image.AsyncImageView;
import com.ss.android.image.FrescoUtils;
import com.ss.android.image.Image;
import com.ss.android.image.Image.UrlItem;

import java.util.ArrayList;
import java.util.Iterator;

public class NightModeAsyncImageView extends AsyncImageView implements NightMode {
    private ArrayList<Uri> mImageUris;
    private boolean mUseImagePolicy;
    private int mBorderColorId;
    private Paint d;
    private boolean mLoadImage4G;

    public NightModeAsyncImageView(Context context) {
        super(context);
        this.mImageUris = new ArrayList();
        this.mBorderColorId = 0;
        this.init(context, null);
    }

    public NightModeAsyncImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mImageUris = new ArrayList();
        this.mBorderColorId = 0;
        this.init(context, attrs);
    }

    public NightModeAsyncImageView(Context context, AttributeSet attrs, int arg4) {
        super(context, attrs, arg4);
        this.mImageUris = new ArrayList();
        this.mBorderColorId = 0;
        this.init(context, attrs);
    }

    public NightModeAsyncImageView(Context context, GenericDraweeHierarchy genericDraweeHierarchy) {
        super(context, genericDraweeHierarchy);
        this.mImageUris = new ArrayList();
        this.mBorderColorId = 0;
        this.init(context, null);
    }

    public final void adaptNightMode(boolean isNightMode) {
        if (isNightMode) {
            ((GenericDraweeHierarchy) this.getHierarchy()).setActualImageColorFilter(TTUtils.getNightModeColorFilter());
        } else {
            ((GenericDraweeHierarchy) this.getHierarchy()).setActualImageColorFilter((ColorFilter) null);
        }

        if (this.mBorderColorId != 0 && this.d != null) {
            this.d.setColor(ContextCompat.getColor(this.getContext(), R.color.ssxinxian1));
            this.invalidate();
        }
    }

    private void init(Context context, AttributeSet attrs) {
        boolean v0 = false;
        boolean v3 = (this.isInEditMode()) || (this.getResources().getConfiguration().uiMode & 48) != 32 ? false : true;
        if (attrs != null) {
            TypedArray v4 = context.obtainStyledAttributes(attrs, com.ss.android.article.base.a.R);
            try {
                this.mBorderColorId = v4.getColor(com.ss.android.article.base.a.S, this.getResources()
                        .getColor(com.ss.android.article.browser.R.color.transparent));
                int v2_1 = v4.getDimensionPixelSize(com.ss.android.article.base.a.T, 0);
                this.mUseImagePolicy = v4.getBoolean(com.ss.android.article.base.a.d, true);
                boolean v5 = v4.getBoolean(com.ss.android.article.base.a.H, false);
                if (this.mBorderColorId == 0) {
                    this.mBorderColorId = v4.getColor(com.ss.android.article.base.a.I,
                            this.getResources().getColor(com.ss.android.article.browser.R.color.transparent));
                }

                if (v2_1 == 0) {
                    v2_1 = v4.getDimensionPixelSize(com.ss.android.article.base.a.J, 0);
                }

                if (v5) {

                } else {
                    this.d = new Paint();
                    this.d.setColor(this.mBorderColorId);
                    this.d.setStrokeWidth(((float) v2_1));
                    this.setPadding(v2_1, v2_1, v2_1, v2_1);
                    this.d.setStyle(Paint.Style.STROKE);
                }
            } catch (Exception v2) {
                v2.printStackTrace();
            }

            v4.recycle();
        } else {
            this.mUseImagePolicy = true;
        }

        if ((this.isInEditMode())
                || (AppData.getInstance().getAbSettings().isLoadImage4G())
                && AppData.getInstance().mNetworkStatusMonitor.mCurrrentNetworkType == NetworkType.MOBILE_4G) {
            v0 = true;
        }

        this.mLoadImage4G = v0;
        this.adaptNightMode(v3);
    }

    public final void setImageURI(Uri uri, @Nullable Object obj) {
        this.mImageUris.clear();
        this.mImageUris.add(uri);
        super.setImageURI(uri, obj);
    }

    public final void setImage(Image image, BaseControllerListener baseControllerListener) {
        this.mImageUris.clear();
        if (!android.support.design.a.isEmptyCollection(image.url_list)) {
            Iterator<UrlItem> iterator = image.url_list.iterator();
            while (iterator.hasNext()) {
                UrlItem item = iterator.next();
                if (item == null) {
                    continue;
                }
                if (android.support.design.a.isTextEmpty(item.url)) {
                    continue;
                }
                this.mImageUris.add(Uri.parse(item.url));
            }
        }
        super.setImage(image, baseControllerListener);
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.mBorderColorId != 0 && this.d != null) {
            canvas.drawRect(0f, 0f, ((float) this.getWidth()), ((float) this.getHeight()), this.d);
        }
    }

    public void setController(DraweeController draweeController) {
        if ((this.mUseImagePolicy)
                && AppData.getInstance().mNetworkStatusMonitor.mCurrrentNetworkType != NetworkType.WIFI
                && AppData.getInstance().mLoadImagePref == 2) {
            Iterator<Uri> iterator = this.mImageUris.iterator();
            int v0;
            do {
                if (iterator.hasNext()) {
                    if (!FrescoUtils.isCached(iterator.next())) {
                        continue;
                    }
                    v0 = 1;
                    break;
                } else {
                    v0 = 0;
                    break;
                }
            }
            while (true);

            if (v0 != 0) {
            } else if (this.mLoadImage4G) {
            } else {
                draweeController = this.getControllerBuilder().setOldController(this.getController()).setUri("").build();
            }
        }
        super.setController(draweeController);
    }

    @Override
    public void f(@NonNull Object o) {

    }
}

